<div class="layuimini-main">
        <button class="layui-btn layui-btn-normal layui-btn-sm" data-open="" data-title="获取批量发货模板" data-full="false"
                data-table="currentTableRenderId"><i class="fa fa-download "></i> 获取批量发货模板
        </button>


        <button class="layui-btn layui-btn-normal layui-btn-sm" data-open="" data-title="批量发货" data-full="false"
                data-table="currentTableRenderId"><i class="fa fa-upload"></i> 批量发货
        </button>

    </div>




<div class="layuimini-container">
    <div class="layuimini-main">
        <table id="currentTable" class="layui-table layui-hide"
               lay-filter="currentTable">
        </table>

        <iframe id="ifile" style="display:none"></iframe>
    </div>
</div>

<script type="text/javascript">

    require(["jquery", "easy-admin"], function ($, ea) {

        var init = {
            table_elem: '#currentTable',
            table_render_id: 'currentTableRenderId',
            index_url: 'order.home/index{if $action}?action={$action}{/if}',
            edit_url: 'order.home/edit',
            batch_url: 'batch_delivery',
            export_url: 'order.home/export',
        };


        ea.table.render({
            init: init,
            toolbar: ['refresh',
                // [{
                //     text: '获取批量发货模板',
                //     url: '',
                //     method: 'open',
                //     auth: 'add',
                //     class: 'layui-btn layui-btn-normal layui-btn-sm',
                //     icon: 'fa fa-download ',
                //     extend: 'data-full="false"',
                // }],
                // [{
                //     text: '批量发货',
                //     url: '',
                //     method: 'open',
                //     auth: 'add',
                //     class: 'layui-btn layui-btn-normal layui-btn-sm',
                //     icon: 'fa fa-upload',
                //     extend: 'data-full="false"',
                // }],
                'export'
            ],
            // toolbar: '#batch_delivery',
            cols: [[
                {type: "checkbox"},
                {field: 'id', minWidth: 30, title: 'ID', searchOp:'='},
                {field: 'order_sn', minWidth: 80, title: '订单号', searchOp:'='},
                {field: 'type', minWidth: 80, title: '订单类型', search:'select', selectList:{0:'普通订单', 1:'拼团订单'}},
                {
                    field: 'goods.goods_id', title: '商品ID', minWidth: 300,
                    templet: function (d) {
                        var titles = '';
                        for (var i = 0; i < d.goods.length; i++) {

                            titles += '<div style="text-align: left;margin-bottom: 5px;border: 1px solid #ddd;">';
                            titles += '<img style="max-width: 200px; max-height: 40px;" src="' + d.goods[i].thumb + '" data-image="undefined"> ';
                            titles += '[' + d.goods[i].goods_id + '] ';
                            titles += d.goods[i].title;
                            titles += ' 【 ¥' + d.goods[i].price + ' 】 数量：' + d.goods[i].total;
                            titles += '</div>';
                        }
                        return titles;
                    }, searchOp:'='
                },

                {field: 'uid', minWidth: 30, title: '购买人ID', searchOp:'='},


                {
                    field: 'status',
                    title: '状态',
                    width: 85,
                    search: 'select',
                    selectList: {"-3": '已退款', "-2": '申请退款', "-1": '取消状态', 0: '待付款', 1: '已付款', 2: '已发货', 3: '已完成', 4:'线下付款审核中'}
                },

                {field: 'create_time', minWidth: 80, title: '创建时间', search: 'range'},

                {
                    field: 'price', minWidth: 150, title: '金额', search: false,
                    templet: function (d) {
                        var titles = '';
                        titles += '<div>';

                        titles += '<div>';
                        titles += '<div style="text-align: left;position: absolute;">';
                        titles += '商品小计: ';
                        titles += '</div>';
                        titles += '<div style="text-align: right;">￥';
                        titles += d.goods_price;
                        titles += '</div>';
                        titles += '</div>';

                        titles += '<div>';
                        titles += '<div style="text-align: left;position: absolute;">';
                        titles += '运费: ';
                        titles += '</div>';
                        titles += '<div style="text-align: right;">￥';
                        titles += d.dispatch_price;
                        titles += '</div>';
                        titles += '</div>';

                        titles += '<div>';
                        titles += '<div style="text-align: left;position: absolute;">';
                        titles += '应收款: ';
                        titles += '</div>';
                        titles += '<div style="text-align: right;color:green;">￥';
                        titles += d.price;
                        titles += '</div>';
                        titles += '</div>';

                        titles += '</div>';
                        return titles;
                    }
                },

                {
                    minWidth: 100,
                    title: '操作',
                    templet: ea.table.tool,
                    operat: [
                        [{
                            text: '查看详细',
                            url: init.edit_url,
                            method: 'open',
                            auth: 'edit',
                            class: 'layui-btn layui-btn-normal',
                            extend: 'data-full="true"',
                        }]
                    ]
                }
            ]],
        });
        // layui.config({
        //     base: 'layui_ext/',
        // }).extend({
        //     excel: 'excel',
        // });
        ea.listen();


        batch = $('button[data-title=获取批量发货模板]')
        // alert(batch.attr('data-title'))
        batch.click(function (obj) {
            obj.stopPropagation()
            checked_data = $('input[name=layTableCheckbox]')
            console.log(checked_data.length)
            // y = $(window).height()-window.screen.height/2;
            // result = confirm('开始下载模版',{offset: y})
            result = confirm('开始下载模版?')
            if (!result) {
                return
            }
            // 获取需要批量发货的订单
            send_data = []
            checked_data.each(function () {
                console.log($(this).text())
                checked_next = $(this).next()
                if (checked_next.hasClass('layui-form-checked')) {
                    // send_data.push(checked_next)
                    tr = $(this).parents('tr')
                    console.log(tr.text())
                    id = tr.children('td[data-field=id]').text()
                    console.log(id)
                    id = parseInt(id)
                    if (!isNaN(id)) {
                        send_data.push(id)
                    }
                }
            })
            console.log(send_data)
            $('#ifile').attr('src', init['batch_url'] + '?ids=' + send_data.join())
        })

        // 上传批量发货excel
        batch_data = $('button[data-title=批量发货]')

        // 绑定上传文件事件
        var upload = layui.upload;
        //执行实例
        var uploadInst = upload.render({
            elem: 'button[data-title=批量发货]' //绑定元素
            ,url: 'batch_delivery_data' //上传接口
            ,accept: 'file'
            ,done: function(res){
                //上传完毕回调
                alert(res.msg)
            }
            ,error: function(){
                //请求异常回调
                alert('批量发货失败，请检查数据，且稍后再试')
            }
        });

        batch_data.click(function (obj) {
            obj.stopPropagation()
        //     checked_data = $('input[name=layTableCheckbox]')
        // // });
        //
        //     // 获取需要批量发货的订单
        //     send_data = []
        //     checked_data.each(function () {
        //         console.log($(this).text())
        //         checked_next = $(this).next()
        //         if (checked_next.hasClass('layui-form-checked')) {
        //             // send_data.push(checked_next)
        //             tr = $(this).parents('tr')
        //             console.log(tr.text())
        //             id = tr.children('td[data-field=id]').text()
        //             console.log(id)
        //             id = parseInt(id)
        //             if (!isNaN(id)) {
        //                 send_data.push(id)
        //             }
        //         }
        //     })
        //     console.log(send_data)
        //     $('#ifile').attr('src', init['batch_url'] + '?ids=' + send_data.join())
        })
    });


</script>